package net.thomaswilburn.intentlab;

import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.twofortyfouram.SharedResources;

public class IntentLabStandalone extends Activity {
	//LOCALE CONSTANTS
	private static final int LOCALE_MENU_SAVE = 1;
	private static final int LOCALE_MENU_CANCEL = 2;
	
	//MY CONSTANTS
	
	//CLASS VARIABLES
	private boolean mCalledFromLocale = false;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String spawnAction = getIntent().getAction();
        if (spawnAction == null) spawnAction = "null";
        
        if (spawnAction.equals(com.twofortyfouram.Intent.ACTION_EDIT_SETTING)) {
        	//This was launched from Locale
        	mCalledFromLocale = true;
        	((LinearLayout) findViewById(R.id.TopParentLayout)).setBackgroundDrawable(SharedResources.getDrawableResource(getPackageManager(), SharedResources.DRAWABLE_LOCALE_BORDER));
        	Log.w("INTENTLAB", "Launched from Locale");
        } else {
        	//We were launched in standalone mode
        	Log.w("INTENTLAB", "Launched from Home");
        }
        
        final Button submit = (Button) findViewById(R.id.SubmitButton);
        submit.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		Log.w("INTENTLAB", "CLICK!");
        		sendIntent();
        	}
        });
        
        final LinearLayout expander = (LinearLayout) findViewById(R.id.expandAdvanced);
        expander.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ImageView icon = (ImageView) findViewById(R.id.expanderIcon);
				LinearLayout advanced = (LinearLayout) findViewById(R.id.advancedOptions);
				if (advanced.getVisibility() == View.GONE) {
					icon.setImageResource(R.drawable.expander_ic_max);
					advanced.setVisibility(View.VISIBLE);
				} else {
					icon.setImageResource(R.drawable.expander_ic_min);
					advanced.setVisibility(View.GONE);
				}
			}
		});
        
        populatePresets();
    }
    
    public boolean onCreateOptionsMenu(final Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	
    	menu.add("Intent Presets");
    	
    	if (mCalledFromLocale) {
    		
    		final PackageManager manager = getPackageManager();

    		final Intent helpIntent = new Intent(com.twofortyfouram.Intent.ACTION_HELP);
    		helpIntent.putExtra(com.twofortyfouram.Intent.EXTRA_STRING_HELP_URL, "http://code.google.com/p/android-underground"); //$NON-NLS-1$

    		// Note: title was set in onCreate
    		helpIntent.putExtra(com.twofortyfouram.Intent.EXTRA_STRING_BREADCRUMB, getTitle().toString());

    		/*
    		 * We are dynamically loading resources from Locale's APK. This will only work if Locale is actually installed
    		 */
    		menu.add(SharedResources.getTextResource(manager, SharedResources.STRING_MENU_HELP))
    			.setIcon(SharedResources.getDrawableResource(manager, SharedResources.DRAWABLE_MENU_HELP)).setIntent(helpIntent);

    		menu.add(0, LOCALE_MENU_CANCEL, 0, SharedResources.getTextResource(manager, SharedResources.STRING_MENU_DONTSAVE))
    			.setIcon(SharedResources.getDrawableResource(manager, SharedResources.DRAWABLE_MENU_DONTSAVE)).getItemId();

    		menu.add(0, LOCALE_MENU_SAVE, 0, SharedResources.getTextResource(manager, SharedResources.STRING_MENU_SAVE))
    			.setIcon(SharedResources.getDrawableResource(manager, SharedResources.DRAWABLE_MENU_SAVE)).getItemId();

    		return true;
    		
    	}
    	
    	return true;
    }
    
    public void populatePresets() {
    	String[] activityActionList = {
    			android.content.Intent.ACTION_ANSWER, 
    			android.content.Intent.ACTION_ATTACH_DATA,
    			android.content.Intent.ACTION_BUG_REPORT,
    			//android.content.Intent.ACTION_CALL,
    			android.content.Intent.ACTION_CALL_BUTTON,
    			android.content.Intent.ACTION_CHOOSER,
    			android.content.Intent.ACTION_CREATE_SHORTCUT,
    			android.content.Intent.ACTION_DEFAULT,
    			android.content.Intent.ACTION_DELETE,
    			android.content.Intent.ACTION_DIAL,
    			android.content.Intent.ACTION_EDIT,
    			android.content.Intent.ACTION_GET_CONTENT,
    			android.content.Intent.ACTION_INSERT,
    			android.content.Intent.ACTION_INSERT_OR_EDIT,
    			android.content.Intent.ACTION_MAIN,
    			android.content.Intent.ACTION_PICK,
    			android.content.Intent.ACTION_POWER_USAGE_SUMMARY,
    			android.content.Intent.ACTION_RUN,
    			android.content.Intent.ACTION_SEARCH,
    			android.content.Intent.ACTION_SEND,
    			android.content.Intent.ACTION_SEND_MULTIPLE,
    			android.content.Intent.ACTION_SENDTO,
    			android.content.Intent.ACTION_SET_WALLPAPER,
    			android.content.Intent.ACTION_SYNC,
    			android.content.Intent.ACTION_VIEW,
    			android.content.Intent.ACTION_VOICE_COMMAND,
    			android.content.Intent.ACTION_WEB_SEARCH,
    			"Custom Action"
    			};
    	
    	Spinner actions = (Spinner) findViewById(R.id.spinIntentAction);
    	ArrayAdapter<String> actionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, activityActionList);
    	//actionAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    	actions.setAdapter(actionAdapter);
    	actions.setPrompt("Choose Intent Action");
    	actions.setSelection(actionAdapter.getPosition(android.content.Intent.ACTION_VIEW));
    	
    	String[] categoryList = {
    			"None",
    			android.content.Intent.CATEGORY_ALTERNATIVE,
    			android.content.Intent.CATEGORY_BROWSABLE,
    			android.content.Intent.CATEGORY_DEFAULT,
    			android.content.Intent.CATEGORY_DEVELOPMENT_PREFERENCE,
    			android.content.Intent.CATEGORY_EMBED,
    			android.content.Intent.CATEGORY_HOME,
    			android.content.Intent.CATEGORY_INFO,
    			android.content.Intent.CATEGORY_LAUNCHER,
    			android.content.Intent.CATEGORY_MONKEY,
    			android.content.Intent.CATEGORY_OPENABLE,
    			android.content.Intent.CATEGORY_PREFERENCE,
    			android.content.Intent.CATEGORY_SAMPLE_CODE,
    			android.content.Intent.CATEGORY_SELECTED_ALTERNATIVE,
    			android.content.Intent.CATEGORY_TAB,
    			android.content.Intent.CATEGORY_TEST,
    			android.content.Intent.CATEGORY_UNIT_TEST,
    			"Custom Category"
    	};
    	
    	
    	Spinner categories = (Spinner) findViewById(R.id.spinIntentCategory);
    	ArrayAdapter<String> catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoryList);
    	//catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	categories.setAdapter(catAdapter);
    	categories.setPrompt("Choose Intent Category");
    	
    	String[] flagTextList = {
    			"None", 
    			"ACTIVITY_BROUGHT_TO_FRONT",
    			"ACTIVITY_CLEAR_TOP",
    			"ACTIVITY_CLEAR_WHEN_TASK_RESET",
    			"ACTIVITY_EXCLUDE_FROM_RECENTS",
    			"ACTIVITY_FORWARD_RESULT",
    			"ACTIVITY_LAUNCHED_FROM_HISTORY",
    			"ACTIVITY_MULTIPLE_TASK",
    			"ACTIVITY_NEW_TASK",
    			"ACTIVITY_NO_HISTORY",
    			"ACTIVITY_NO_USER_ACTION",
    			"ACTIVITY_PREVIOUS_IS_TOP",
    			"ACTIVITY_REORDER_TO_FRONT",
    			"ACTIVITY_RESET_TASK_IF_NEEDED",
    			"ACTIVITY_SINGLE_TOP"
    	};
    	
    	Spinner flags = (Spinner) findViewById(R.id.spinIntentFlag);
		ArrayAdapter<String> flagTextAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, flagTextList);
		flags.setAdapter(flagTextAdapter);
		flags.setPrompt("Choose Intent Flag");
    }
    
    public void sendIntent() {
    	boolean problem = false;
    	
    	Intent dispatch = new Intent();
    	
    	dispatch.setAction((String) ((Spinner) findViewById(R.id.spinIntentAction)).getSelectedItem());
    	
    	EditText txtData = (EditText) findViewById(R.id.txtIntentData);
    	if (txtData.length() > 0) {
    		dispatch.setData(Uri.parse(txtData.getText().toString()));
    	}
    	
    	Spinner category = (Spinner) findViewById(R.id.spinIntentCategory);
    	if (!category.getSelectedItem().equals("None")) {
    		dispatch.addCategory((String) category.getSelectedItem());
    	}
    	
    	EditText txtType = (EditText) findViewById(R.id.txtIntentType);
    	if (txtType.length() > 0) {
    		dispatch.setType(txtType.getText().toString());
    	}
    	
    	EditText txtCompPackage = (EditText) findViewById(R.id.txtCompPackage);
    	EditText txtCompClass = (EditText) findViewById(R.id.txtCompClass);
    	if (txtCompPackage.length() > 0 && txtCompClass.length() > 0) {
    		dispatch.setComponent(new ComponentName(txtCompPackage.getText().toString(), txtCompClass.getText().toString()));
    	} else if (txtCompPackage.length() + txtCompClass.length() > 0) {
    		Toast.makeText(this, "Incomplete component name", Toast.LENGTH_SHORT).show();
    		problem = true;
    	}
    	
    	int[] flagNumList = {
    			0, 
    			android.content.Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT,
    			android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP,
    			android.content.Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET,
    			android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS,
    			android.content.Intent.FLAG_ACTIVITY_FORWARD_RESULT,
    			android.content.Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY,
    			android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK,
    			android.content.Intent.FLAG_ACTIVITY_NEW_TASK,
    			android.content.Intent.FLAG_ACTIVITY_NO_HISTORY,
    			android.content.Intent.FLAG_ACTIVITY_NO_USER_ACTION,
    			android.content.Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP,
    			android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT,
    			android.content.Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED,
    			android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP
    	};
    	
    	long flagID = ((Spinner) findViewById(R.id.spinIntentFlag)).getSelectedItemId();
    	if (flagID > 0) {
    		dispatch.setFlags(flagNumList[(int) flagID]);
    	}
    	
    	if (problem) return;
    	
    	try {
    		startActivity(dispatch);
    	} catch (Exception err) {
    		Toast.makeText(this, err.toString(), Toast.LENGTH_LONG).show();
    	}
    }
    
    /*public Dialog onCreateDialog(int id) {
    	
    }*/
}